package com.boot.controller;

import com.boot.token.MyToken;
import com.boot.util.JwtUtils;
import com.boot.util.ResultUtils;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class LoginController {


    @RequestMapping("/login")
    public String login(String userName,String password) {
        String token = JwtUtils.getToken(userName);

        MyToken myAuthenticationToken = new MyToken(token,userName,password);

        Subject subject = SecurityUtils.getSubject();

        try {

            subject.login(myAuthenticationToken);
            return ResultUtils.success(token);
        }catch (UnknownAccountException e){
            return ResultUtils.error("UnknownAccount");
        }catch (IncorrectCredentialsException e){
            return ResultUtils.error("IncorrectCredentials");
        }catch (Exception e){
            return ResultUtils.error("500");
        }
    }
}

